package com.bluefox.hospital.convertor;

import com.bluefox.common.web.dto.AbstractConvertor;
import com.bluefox.hospital.dto.DepartmentDTO;
import com.bluefox.hospital.model.Department;
import com.bluefox.hospital.service.DepartmentService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * DepartmentConvertor
 */
@Component
public class DepartmentConvertor extends AbstractConvertor<Department, DepartmentDTO> {

    @Autowired
    private DepartmentService departmentService;

    @Override
    public Department toModel(@NonNull final DepartmentDTO dto) {
        if (dto.isNew()) {//新增
            return constructModel(dto);
        } else {//更新
            return updateModel(dto);
        }
    }

    @Override
    public DepartmentDTO toDTO(@NonNull final Department model, final boolean forListView) {
        final DepartmentDTO dto = new DepartmentDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setRemark(model.getRemark());
        dto.setStatus(model.getStatus());
        dto.setLabel(model.getName());
        dto.setValue(model.getName());
        dto.setDepartmentOrder(model.getDepartmentOrder());
        dto.setIsKeyDepartment(model.getIsKeyDepartment());
        dto.setRoomNumber(model.getRoomNumber());
        dto.setDoctors(model.getDoctors());
        return dto;
    }

    // 构建新Model
    private Department constructModel(final DepartmentDTO dto) {
        Department model = new Department();
        model.setName(dto.getName());
        model.setRemark(dto.getRemark());
        model.setStatus(dto.getStatus());

        model.setIsKeyDepartment(dto.getIsKeyDepartment());
        model.setRoomNumber(dto.getRoomNumber());
        model.setDoctors(dto.getDoctors());
        return model;
    }

    // 更新Model
    private Department updateModel(final DepartmentDTO dto) {
        Department model = departmentService.get(dto.getId());
        model.setName(dto.getName());
        model.setRemark(dto.getRemark());
        model.setStatus(dto.getStatus());

        model.setIsKeyDepartment(dto.getIsKeyDepartment());
        model.setRoomNumber(dto.getRoomNumber());
        model.setDoctors(dto.getDoctors());
        return model;
    }

    /**
     * 判断是不是叶子节点
     *
     * @param dtos
     * @param id
     * @return
     */
  /*  public Boolean judgeEle(List<DepartmentDTO> dtos, Long id) {
        for (int i = 0; i < dtos.size(); i++) {
            if (id.equals(dtos.get(i).getParentId())) {
                return true;
            }
        }
        return false;
    }

    public List<DepartmentDTO> recombineTree(List<DepartmentDTO> dtos, Long id) {
        List<DepartmentDTO> list = new ArrayList<DepartmentDTO>();
        for (int i = 0; i < dtos.size(); i++) {
            if (id.equals(dtos.get(i).getParentId())) {
                if (!judgeEle(dtos, dtos.get(i).getParentId())) {
                    if (null != recombineTree(dtos, dtos.get(i).getId()) && recombineTree(dtos, dtos.get(i).getId()).size() == 0) {

                    } else {
                        dtos.get(i).setChildren(recombineTree(dtos, dtos.get(i).getId()));
                    }
                }
                list.add(dtos.get(i));
            }
        }
        return list;

    }*/
}
